$(document).ready(function () {

    if ($("#pagehistory")) {

        var len = window.history.length;
        var pagehistory = $("#pagehistory").val();

        if (pagehistory == "") {
            $("#pagehistory").val(len);
            if (navigator.vendor == "Google Inc." || navigator.vendor == "Apple Computer, Inc.") {
                GetBasketSessionValues();
            }
        }
        else {
            GetBasketSessionValues();
        }
    }

    //ajax action overlay
    $('#loadingDiv')
    .hide()  // hide it initially
    .ajaxSend(function (event, xhr, options) {

        if (options.extradelay) { $(this).attr('extradelay', options.extradelay); } else { $(this).attr('extradelay', 0); }
        var dheight = $(document).height();
        var wheight = ($(window).height() / 2) + ($(window).scrollTop() - 75);
        $(this).css({ 'width': '100%', 'height': dheight + 'px', 'background-position': '50% ' + wheight + 'px' }).show();
    })
    .ajaxStop(function () {
        try {
            var extradelay = 0;
            if (!isNaN($(this).attr('extradelay'))) { extradelay = parseInt($(this).attr('extradelay')) };
            var self = $(this); //can't use $(this) in the timeout function
            setTimeout(function () {
                self.css({ 'width': '0px', 'height': '0px', 'background-position': 'center center' }).hide();
            }, extradelay);
        } catch (e) {
            self.css({ 'width': '0px', 'height': '0px', 'background-position': 'center center' }).hide();
        }
    });
});


function GetBasketSessionValues() {
    var url = "/ajax/basket";

    var postdata = "EventType=getBasketValues";
    postdata = encodeURI(postdata);

    $.ajax({
        dataType: "json",
        type: "POST",
        url: url,
        async: true,
        data: postdata,
        success: function (data) {
            var o = jQuery.parseJSON(data);
            if (o.IsNet) {
                $("#spnBasketTotal").html(o.CurrencySymbol + o.priceNet);
            }
            else {
                $("#spnBasketTotal").html(o.CurrencySymbol + o.priceGross);
            }
            $("#spnBasketCount").html(o.qty);
        }
    });
}

//postdata += "& =" + ;

function addtoBasket(productInstance)
{
    var url = "/ajax/basket";
    var productid = $("#stockcode_" + productInstance).val();
    var Qty = $("#Qty" + productInstance).val();
    var note = $("#note" + productInstance).val();
    var isMatrix = $("#isMatrix_" + productInstance).val();

    var postdata = "EventType=addToBasket";
    postdata += "&isAddToBasketQty=" + $("#Qty" + productInstance).attr("isAddToBasketQty");
    postdata += "&stockcode=" + productid;
    postdata += "&quantity=" + Qty;
    postdata += "&note=" + note;

    var fail = false;

    if (isMatrix == "True") {
        var axisCount = $("#AxisCount_" + productInstance).val();
        //are all values selected?
        for (var i = 1; i <= axisCount; i++) {
            if (typeof ($("#Matrix" + i + "_" + productInstance).val()) != 'undefined') {
                try {
                    var coord = parseInt($("#value" + i + "_" + productInstance).val());
                    if (coord > 0) { postdata += "&coords" + i + "=" + coord; }
                    else { fail = true; }
                }
                catch (err)
                { fail = true; }
            }
            else
            {
                fail = true;
            }
        }
    }
    postdata = encodeURI(postdata);

    if (!fail) {
        $.ajax({
            dataType: "json",
            type: "POST",
            url: url,
            async: true,
            data: postdata,
            success: function (data) {
                var o = jQuery.parseJSON(data);
                if (o.IsNet) {
                    $("#spnBasketTotal").html(o.CurrencySymbol + o.priceNet);
                }
                else {
                    $("#spnBasketTotal").html(o.CurrencySymbol + o.priceGross);
                }
                $("#spnBasketCount").html(o.qty);
            }
        });
    }
}

function searchSubmit() {
    var searchGroupListCats = $("#searchGroupListCats").val();
    var searchGroupListBrands = $("#searchGroupListBrands").val();
    var searchGroupList = $("#searchGroupList").val();
    var qs = "";

    if ($("#keyword").val() != $("#defaultSearch").val()) {
        if ($("#keyword").val() != "") {
            if (qs == ""){ qs = "?"}
            qs += 'q=' + encodeURIComponent($("#keyword").val().replace(/ /g, "+"));
        }
    }

    if (typeof searchGroupListCats !== "undefined") {
        if (qs == ""){ qs = "?"}else{qs += "&"}
        qs += "category=" + searchGroupListCats
    }
    if (typeof searchGroupListBrands !== "undefined") {
        if (qs == "") { qs = "?" } else { qs += "&" }
        qs += "brand=" + searchGroupListBrands
    }
    if (typeof searchGroupList !== "undefined") {
        if (qs == "") { qs = "?" } else { qs += "&" }
        if (searchGroupList.substring(0, 1) == "c") {
            qs += "category=" + searchGroupList
        }
        else
        {
            qs += "brand=" + searchGroupList
        }
    }
    if (qs != "") { window.location = '/search' + qs }
}

function filterSelect(selectparent, selectChild)
{
    var url = "/ajax/data";
    var postdata = "EventType=filterSelect";
    postdata += "&selectparent=" + selectparent;
    postdata += "&selectChild=" + selectChild;
    postdata += "&selectparentID=" + $("#" + selectparent).val();
    postdata += "&selectChildID=" + $("#" + selectChild).val();
    postdata = encodeURI(postdata);

    $.ajax({
        dataType: "text",
        type: "POST",
        url: url,
        async: true,
        data: postdata,
        success: function (data) {
            var o = jQuery.parseJSON(data);
            $("#" + selectChild).empty();
            $("#" + selectChild).html(o.options);
        }
    });
}

function fnTrapKD(btn, event) {

    var onclickHandler = document.getElementById(btn).getAttribute('onclick');

    if (document.all) {
        if (event.keyCode == 13) {
            event.returnValue = false;
            event.cancel = true;
            eval(onclickHandler);
        }
    }
    else if (document.getElementById) {
        if (event.which == 13) {
            event.returnValue = false;
            event.cancel = true;
            eval(onclickHandler);
        }
    }
    else if (document.layers) {
        if (event.which == 13) {
            event.returnValue = false;
            event.cancel = true;
            eval(onclickHandler);
        }
    }
}


